package com.yyhh.helpcall.server;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.yyhh.helpcall.model.MessageModel;
import com.yyhh.helpcall.utils.AppUtils;
import com.yyhh.helpcall.utils.NetLog;
import com.umeng.analytics.MobclickAgent;
import java.util.HashMap;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.service.JPushMessageReceiver;

public class JiGuangPushReceive extends JPushMessageReceiver {

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        processCustomMessage(context, customMessage);
    }

    //send msg to MainActivity
    private void processCustomMessage(Context context, CustomMessage customMessage) {
        String message = customMessage.message;
        String extras = customMessage.extra;
        NetLog.d("onReceive push message extras = " + extras + " message = " + message);
        try {
            //extras = {"data":{"action":1,"content":"cancel_call","customer_id":38,"msg_id":"1095","phone":"15012585612"},"type":"cancel_call"}
            MessageModel messageModel = new Gson().fromJson(extras, MessageModel.class);
            Log.d("netlog-jiguang-pushType",messageModel.type);
            switch (messageModel.type) {
                case "call_phone":
                    if (!TextUtils.isEmpty(messageModel.data.phone)) {
                        AppUtils.callPhone(context, messageModel.data.phone,messageModel.data.msg_id,"极光推送");
                        //发送事件
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("phone", messageModel.data.phone);
                        MobclickAgent.onEventObject(context, "callPhone", map);
                    }
                    break;
                case "cancel_call":
                    AppUtils.endCall(context,messageModel.data.msg_id,messageModel.data.phone,"极光推送");
                    //发送事件
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("phone", messageModel.data.phone);
                    MobclickAgent.onEventObject(context, "cancleCall", map);
                    break;
                default:
                    break;
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
